package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    //查询客户列表数据
    @GetMapping
    public Result customerList(CustomerDto customerDto){
        log.info("查询客户列表:{}" , customerDto);
        PageResult<Customer> pageResult = customerService.list(customerDto);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result addCustomer(@RequestBody Customer customer){
        log.info("添加客户:{}" , customer);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询客户id:{}" , id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }
    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        log.info("更新客户:{}" , customer);
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }






}
